package model.objects;

import java.awt.Graphics;

import javax.swing.JPanel;

import model.general.Location;
import model.move.IMove;
import model.move.MoveBoat;
import model.observer.Obseverable;

public class Boat extends Sprites implements IAnimateMainActor {

	private IMove move;
	private Hook hook;
	private boolean isMoveLeft = false;
	private int life, score;

	// private static final String DEFAUT_IMG_BOAT_LEFT = "boat-left.png";
	// private static final String DEFAUT_IMG_BOAT_RIGHT = "boat-right.png";
	private static String DEFAUT_IMG_BOAT_LEFT = "";
	private static String DEFAUT_IMG_BOAT_RIGHT = "";

	public Boat(Location location, String srcImgLeft, String srcImgRight,
			JPanel pnl) {
		super(location, srcImgLeft, srcImgRight, pnl);
		this.move = new MoveBoat(location);
		this.DEFAUT_IMG_BOAT_LEFT = srcImgLeft;
		this.DEFAUT_IMG_BOAT_RIGHT = srcImgRight;
		this.hook = new Hook(new Location(45, 150), "hook.png", "hook.png", pnl);
		hook.setBoat(this);
	}

	@Override
	public void show(Graphics g) {
		g.drawImage(img, location.getX(), location.getY(), null);
		hook.show(g);
	}

	@Override
	public void moveDown() {
		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				hook.isMove(true);
				hook.move();
			}
		}).start();

	}

	@Override
	public void moveLeft() {
		if (!hasOutPanel()) {
			isMoveLeft = true;
			isChangeImage();
			move.moveLeft();
			hook.updateLocation(location);
		}

	}

	@Override
	public void moveRight() {
		if (!hasOutPanel()) {
			isMoveLeft = false;
			isChangeImage();
			move.moveRight();
			hook.updateLocation(location);
		}

	}

	@Override
	public IAnimateSubActor getHook() {

		return this.hook;
	}

	public void addLife(int add) {
		this.life += add;
	}

	public void addScore(int add) {
		this.score += score;
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public boolean hasOutPanel() {
		int min = this.pnl.getX();
		int max = this.pnl.getX() + this.pnl.getWidth() - this.width;
		if ((location.getX() < min)) {
			this.location.setX(min);
			return true;
		}
		if ((location.getX() > max)) {
			this.location.setX(max);
			return true;
		}
		return false;
	}

	private void isChangeImage() {
		// TODO Auto-generated method stub
		if (isMoveLeft == true) {
			img = loadImage.getbufferImage(srcImgLeft);
		}
		if (isMoveLeft == false) {
			img = loadImage.getbufferImage(srcImgRight);
		}
	}

	public Location getLocation() {
		return location;
	}

	@Override
	public Obseverable getObserable() {
		// TODO Auto-generated method stub
		return this.hook;
	}

	public int getLife() {
		return life;
	}

	public void setLife(int life) {
		this.life = life;
	}

	public int getScore() {
		return score;
	}

	public void setScore(int score) {
		this.score = score;
	}
	
	
}
